package com.example.quartzdemo.repository.impl;

import com.example.quartzdemo.dto.JobInfo;
import com.example.quartzdemo.repository.JobRepository;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author jasondai
 * @date 2023/10/27 14:19
 */
@Repository
public class JobRepositoryImpl implements JobRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public List<JobInfo> queryJobs(){
        String sql="\t\tSELECT\n" +
                "\t\t\t\tqrtz_job_details.JOB_NAME,\n" +
                "\t\t\t\tqrtz_job_details.JOB_GROUP,\n" +
                "\t\t\t\tqrtz_job_details.JOB_CLASS_NAME,\n" +
                "\t\t\t\tqrtz_triggers.TRIGGER_NAME,\n" +
                "\t\t\t\tqrtz_triggers.TRIGGER_GROUP,\n" +
                "\t\t\t\tqrtz_cron_triggers.CRON_EXPRESSION,\n" +
                "\t\t\t\tqrtz_cron_triggers.TIME_ZONE_ID\n" +
                "\t\t\tFROM\n" +
                "\t\t\t\tqrtz_job_details\n" +
                "\t\t\tJOIN qrtz_triggers\n" +
                "\t\t\tJOIN qrtz_cron_triggers ON qrtz_job_details.JOB_NAME = qrtz_triggers.JOB_NAME\n" +
                "\t\t\tAND qrtz_triggers.TRIGGER_NAME = qrtz_cron_triggers.TRIGGER_NAME\n" +
                "\t\t\tAND qrtz_triggers.TRIGGER_GROUP = qrtz_cron_triggers.TRIGGER_GROUP";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            JobInfo jobInfo = new JobInfo();
            jobInfo.setJobName(rs.getString("JOB_NAME"));
            jobInfo.setJobGroup(rs.getString("JOB_GROUP"));
            jobInfo.setJobClassName(rs.getString("JOB_CLASS_NAME"));
            jobInfo.setTriggerName(rs.getString("TRIGGER_NAME"));
            jobInfo.setTriggerGroup(rs.getString("TRIGGER_GROUP"));
            jobInfo.setCronExpression(rs.getString("CRON_EXPRESSION"));
            jobInfo.setTimeZoneId(rs.getString("TIME_ZONE_ID"));

            return jobInfo;
        });
    }
}
